#define _CRT_SECURE_NO_WARNINGS

void _preorderTraversal(struct TreeNode* root, int* returnSize, int** a, int* n)
{
    if (root == NULL)
        return;
    if (*returnSize == *n)
    {
        *a = (int*)realloc(*a, sizeof(int) * (*n) * 2);
        (*n) *= 2;
    }
    (*a)[(*returnSize)++] = root->val;
    _preorderTraversal(root->left, returnSize, a, n);
    _preorderTraversal(root->right, returnSize, a, n);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    if (root == NULL)
    {
        *returnSize = 0;
        return NULL;
    }
    int n = 4;
    *returnSize = 0;
    int* a = (int*)malloc(sizeof(int) * n);
    _preorderTraversal(root, returnSize, &a, &n);
    return a;
}